package com.sdksoft.readcard.utils

import okhttp3.Dns
import java.net.InetAddress
import java.net.UnknownHostException
import java.util.*
import java.util.concurrent.FutureTask
import java.util.concurrent.TimeUnit

/**
 * DNS解析超时时间设置
 * @author mtj
 */
class DnsTimeOut(private val timeout: Long, private val timeUnit: TimeUnit) : Dns {
    @Throws(UnknownHostException::class)
    override fun lookup(hostname: String): List<InetAddress> {
        return try {
            val task = FutureTask { InetAddress.getAllByName(hostname).toList() }
            Thread(task).start()
            task[timeout, timeUnit]
        } catch (var4: Exception) {
            val unknownHostException = UnknownHostException("Unable to resolve host $hostname")
            unknownHostException.initCause(var4)
            throw unknownHostException
        }
    }
}